package com.nmf.site.common.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.nmf.site.common.excep.KideException;

public class KideExceptionResolver extends DefaultHandlerExceptionResolver {

	private static final Logger logger = LoggerFactory.getLogger(KideExceptionResolver.class);


	public KideExceptionResolver() {
		setOrder(HIGHEST_PRECEDENCE);
	}

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// 鍏堣褰曟棩蹇�
		logger.error("handle request error. uri: " + request.getRequestURI() + " msg: " + ex.getMessage(), ex);
		if (ex instanceof KideException) {
			KideException be = (KideException) ex;
			ModelAndView result = new ModelAndView(new FastJsonJsonView());
			result.addObject("status", be.getCode().value);
			result.addObject("message", be.getCode().name+be.getMessage());
			
			return result;
			
		}  else {
			return super.resolveException(request, response, handler, ex);
		}
		
	}

}
